<?php

require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

/**
 * BRail Application Test Case
 */
class BRail_Application_TestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $application;

    /**
     * @var array contains the settings from application/config/test.ini
     */
    public $options;

    /**
     * Initialize Test Case for Zend Application
     *
     * Bootstrap application for each test, and put test.ini settings into $this->options
     *
     * @return void
     */
    public function setUp()
    {
        require_once 'Zend/Loader/Autoloader.php';
        Zend_Loader_Autoloader::resetInstance();
        require_once 'Application.php';

        // Create application, bootstrap, and run
        $this->application = new Application(
                APPLICATION_ENV,
                APPLICATION_PATH . '/configs/application.ini'
        );

        // Until they continue to do that in parent::setUp() and not is_subclass_of
        // if ($this->bootstrap instanceof Zend_Application)
        // We're obliged to $this->application->bootstrap() by ourselves
        $this->bootstrap = array($this->application, 'bootstrap');
        $this->options = parse_ini_file(APPLICATION_PATH . '/configs/test.ini');
        parent::setUp();
    }

    /**
     * Returns the authentication token from a form in the current page
     * @return null|string first authentication_token or null if none is found
     */
    protected function getAuthenticationToken()
    {
        if (!preg_match(
        		'/<input type="hidden" name="authentication_token" value="([\da-z]{32})"/',
        		$this->getResponse()->outputBody(),
        		$matches)
        	) {
            throw new Exception("No authentication is found in the page");
        }
        return $matches[1];
    }
}